
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllColor',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapColorToView(responseContent);
        }
    });

}

function bindEvent() {
    ISS.EVENT.bindEvent('#RegeditForm_color_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Thêm màu mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#RegeditForm_color_Modified', 'click', function () {
        toEditPage();
    });

    ISS.EVENT.bindEvent('#RegeditForm_color_Delete', 'click', function () {
        deleteColor();
    });
}

function deleteColor() {
    var _maMaus = [];
    var _colors = [];
    ISS.DOM.forEach('#form-RegeditFormColor input[type="checkbox"]:checked', function(){
        _maMaus.push($(this).val());
    });

    if(_maMaus.length > 0) {
        for(var i = 0; i < _maMaus.length; i++) {
            var color = {};
            color.maMau = _maMaus[i];
            _colors.push(color);
        }

        ISS.BACKEND.request({
            url: '../../service/admin/header/deleteColor',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(_colors),
            success: function (data) {
                var responseContent = data.responseObject;
                if(data.code == 0) {
                    alert('Xoá thành công !');
                    loadData();
                }
                else {
                    alert('Xoá không thành công !');
                }
            }
        });
    }
    else {
        alert('Chưa chọn dòng nào');
    }
}

function sendInfo() {
    var color = {};

    color.maMau = ISS.DOM.getVal('#RegeditFormColor_code');
    color.tenMau = ISS.DOM.getVal('#RegeditFormColor_name');

    ISS.BACKEND.request({
        url: '../../service/admin/header/registerColor',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(color),
        success: function (data) {
            var responseContent = data.responseObject;
            if(data.code == 0) {
                alert('Thêm thành công !');
                ISS.WINDOW.location('/jsp/admin/ad-product-color-common.jsp');
            }
            else {
                alert('Thêm không thành công !');
            }
        }
    });
}

function toEditPage() {
    var _maMau = [];
    ISS.DOM.forEach('#form-RegeditFormColor input[type="checkbox"]:checked', function(){
        _maMau.push($(this).val());
    });

    if(_maMau.length == 1) {
        var _url = '/service/admin/toProductColorPage/' + _maMau[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Không được chọn hơn một dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#RegeditFormColor_code', 'error');
    ISS.DOM.removeClass('#RegeditFormColor_name', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapColorToView(color) {

    var rows = color.length;
    var columns = 1;
    var str = '';
    str += '<table id="ColorTable" class="tableColorWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnColorSTT">STT</th>';
    str += '       <th class="columnColorStick">Tick</th>';
    str += '       <th class="columnColorCode">Mã màu</th>';
    str += '       <th class="columnColorName">Tên màu</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }else{
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnColorSTT tableColorPadding">' + i + '</td>';
            str += '<td class="columnColorStick tableColorPadding"><input onclick="highlight_row(this)" id="' + color[i].maMau + '" type="checkbox" value="' + color[i].maMau + '"/></td>';
            str += '<td class="columnColorCode tableColorPadding">' + color[i].maMau + '</td>';
            str += '<td class="columnColorName tableColorPadding">' + color[i].tenMau + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#RegeditFormColor_All', str);
}